package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.ReportMapper;
import com.sky.result.Result;
import com.sky.service.ReportService;
import com.sky.utils.StringUtil;
import com.sky.vo.TurnoverReportVO;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ReportServiceImp implements ReportService {
    @Autowired
    ReportMapper reportMapper;
    @Override
    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {

        ArrayList<LocalDate> datelist = new ArrayList<>();
        datelist.add(begin);
        while(true){
            if(!begin.equals(end)){
                begin = begin.plusDays(1);
                datelist.add(begin);
            }else
                break;
        }

        //根据每天的日期去查询营业额 因为营业额的日期是LocalDatetime类型所以需要转化
        List<Double> turnoverList = datelist.stream().map(date ->
                {
                    LocalDateTime minTime = LocalDateTime.of(date, LocalTime.MIN);
                    LocalDateTime maxTime = LocalDateTime.of(date, LocalTime.MAX);
                    int status = Orders.COMPLETED;
                    Double turnover = reportMapper.getTurnoverByDate(minTime, maxTime, status);


                    if(turnover == null)
                        turnover = 0.0;

                    return turnover ;
                }
        ).collect(Collectors.toList());

        return  TurnoverReportVO.builder().dateList(StringUtils.join(datelist, ","))
                .turnoverList(StringUtils.join(turnoverList, ",")).build();
    }
}
